package com.pocketsweet.ui.fragments;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.GetCallback;
import com.lidroid.xutils.ViewUtils;
import com.pocketsweet.R;
import com.pocketsweet.model.MLUser;
import com.pocketsweet.model.MLVoice;
import com.pocketsweet.service.UserService;
import com.pocketsweet.ui.Main;
import com.pocketsweet.ui.uilib.FlowLayout;
import com.pocketsweet.ui.uilib.LoadingDailog;
import com.pocketsweet.ui.uilib.RandomColorWillingTag;
import com.pocketsweet.ui.uilib.adapter.VoiceAdapter;
import com.pocketsweet.utils.StringUtils;
import com.pocketsweet.utils.ToolKits;

public class LoveSkillFragment extends Fragment implements OnItemClickListener {

	private TextView tvBelong;
	private TextView tvLikeType;
	private TextView tvCallMe;
	private TextView tvCallYou;
	private LinearLayout linBelong;
	private LinearLayout linLikeType;
	private LinearLayout linCallMe;
	private LinearLayout linCallYou;
	private LinearLayout linVoice;
	private LinearLayout linWantU;
	private FlowLayout willingTag;
	private ListView voiceListView;
	String userId;
	private LoadingDailog loading;
	private VoiceAdapter voiceAdapter;
	private MLUser mlUser = new MLUser();
	private ArrayList<String> calledList = new ArrayList<String>();// 将值传给编辑页面，编辑页面不用再请求网络
	private ArrayList<String> callList = new ArrayList<String>();// 将值传给编辑页面，编辑页面不用再请求网络
	private ArrayList<String> willingList = new ArrayList<String>();; // 将值传给编辑页面，编辑页面不用再请求网络

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_love_skill, null);
		ViewUtils.inject(this, rootView);
		return rootView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Intent intent = getActivity().getIntent();
		userId = intent.getStringExtra("userId");
		initView();
		if (!userId.equals(UserService.getCurrentUserId())) {
			getUser();
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		if (userId.equals(UserService.getCurrentUserId())) {
			getUser();
		}
		// getUserInfo();
	}

	@Override
	public void onStart() {
		super.onStart();
	}

	@Override
	public void onPause() {
		super.onPause();
		if (Main.MediaTimer != null && Main.mMediaPlayer.isPlaying()) {
			Main.mMediaPlayer.pause();
			Main.mMediaPlayer.stop();
			Main.mMediaPlayer.reset();
			Main.MediaTimer.cancel();
		}
	}

	@Override
	public void onStop() {
		super.onStop();

	}

	private void getUser() {
		loading = ToolKits.createLoadingDialog(getActivity(), "提交中..");
		loading.setText("加载中");
		loading.show();
		if (userId == null) {
			userId = UserService.getCurrentUserId();
		}
		if (!userId.equals(UserService.getCurrentUserId())) {
			AVQuery<MLUser> query = new AVQuery<MLUser>("_User");
			query.getInBackground(userId, new GetCallback<MLUser>() {
				@Override
				public void done(MLUser user, AVException e) {
					if (e == null) {
						mlUser = user;
						initVoice();
					} else {
						// e.printStackTrace();
						loading.dismiss();
					}
				}
			});
		} else {
			mlUser = UserService.getCurrentUser();
			initVoice();
		}
	}

	private void initView() {
		tvBelong = (TextView) getActivity().findViewById(R.id.tvBelong);
		tvLikeType = (TextView) getActivity().findViewById(R.id.tvLikeType);
		tvCallMe = (TextView) getActivity().findViewById(R.id.tvCallMe);
		tvCallYou = (TextView) getActivity().findViewById(R.id.tvCallYou);
		linBelong = (LinearLayout) getActivity().findViewById(R.id.linBelong);
		linLikeType = (LinearLayout) getActivity().findViewById(
				R.id.linLikeType);
		linCallMe = (LinearLayout) getActivity().findViewById(R.id.linCallMe);
		linCallYou = (LinearLayout) getActivity().findViewById(R.id.linCallYou);
		linWantU = (LinearLayout) getActivity().findViewById(R.id.linWantU);
		linVoice = (LinearLayout) getActivity().findViewById(R.id.linVoice);
		willingTag = (FlowLayout) getActivity().findViewById(R.id.willingTag);
		voiceListView = (ListView) getActivity().findViewById(
				R.id.voiceListView);
		voiceAdapter = new VoiceAdapter(getActivity(), Main.voiceList, false);
		voiceListView.setAdapter(voiceAdapter);
	}

	private void initVoice() {
		// if (Main.voiceList == null) {
		final AVQuery<MLVoice> query = new AVQuery<MLVoice>("MLVoice");
		query.whereEqualTo("belong_to", mlUser);
		query.findInBackground(new FindCallback<MLVoice>() {
			@Override
			public void done(List<MLVoice> list, AVException arg1) {
				if (arg1 == null) {
					Main.voiceList.clear();
					if (list.size() > 0) {
						for (MLVoice temp : list) {
							Main.voiceList.add(temp);
						}
						voiceAdapter.notifyDataSetChanged();
						linVoice.setVisibility(View.VISIBLE);
					} else {
						linVoice.setVisibility(View.GONE);
					}
					getUserInfo();
				} else {
					getUserInfo();
					// Log.e("＝＝错误", "查询错误: " + arg1.getMessage());

				}
			}
		});
		// }
	}

	/*
	 * 个人资料
	 */
	private void getUserInfo() {
		voiceAdapter.notifyDataSetChanged();
		if (!StringUtils.isEmpty(mlUser.getBelong())) {
			tvBelong.setText(mlUser.getBelong());
			linBelong.setVisibility(View.VISIBLE);
		} else {
			linBelong.setVisibility(View.GONE);
		}
		if (!StringUtils.isEmpty(mlUser.getLike())) {
			tvLikeType.setText(mlUser.getLike());
			linLikeType.setVisibility(View.VISIBLE);
		} else {
			linLikeType.setVisibility(View.GONE);
		}
		if (mlUser.getCalledTags() != null && mlUser.getCalledTags().size() > 0) {

			StringBuffer called = new StringBuffer();
			for (String temp : mlUser.getCalledTags()) {
				called.append(temp + " ");
			}
			tvCallMe.setText(called);
			linCallMe.setVisibility(View.VISIBLE);
		} else {
			linCallMe.setVisibility(View.GONE);
		}
		if (mlUser.getCallTags() != null && mlUser.getCallTags().size() > 0) {

			StringBuffer call = new StringBuffer();
			for (String temp : mlUser.getCallTags()) {
				call.append(temp + " ");
			}
			tvCallYou.setText(call);
			linCallYou.setVisibility(View.VISIBLE);
		} else {
			linCallYou.setVisibility(View.GONE);
		}
		if (mlUser.getWillingTags() != null
				&& mlUser.getWillingTags().size() > 0) {
			willingList = new ArrayList<String>();
			for (String temp : mlUser.getWillingTags()) {
				willingList.add(temp);
			}
			initWillingTag(willingList);
			linWantU.setVisibility(View.VISIBLE);
		} else {
			linWantU.setVisibility(View.GONE);
		}
		if (loading.isShowing()) {
			loading.dismiss();
		}
	}

	private void initWillingTag(ArrayList<String> willingList) {
		willingTag.removeAllViews();
		for (int i = 0; i < willingList.size(); i++) {
			if (getActivity() != null) {
				RandomColorWillingTag itemView = new RandomColorWillingTag(
						getActivity());
				itemView.setText(willingList.get(i));
				willingTag.addView(itemView);
			}
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO 这里点击之后进去资料或者聊天页面

	}
}